﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using BITC.Library.Provider;
using VVA.Data.Entity;

namespace VVA.Data.Service
{
    public class ArticleService
    {
        #region Constructor

        #endregion

        #region Method

        public Article GetByID(int id)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Article>("Proc_Article_SelectById", id).FirstOrDefault();
        }

        public Article GetBySlug(string slug)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Article>("Proc_Article_SelectBySlug", slug).FirstOrDefault();
        }

        public IList<Article> GetAll(string culture, int? pageIndex, int pageSize, out int totalCount)
        {            
            var _db = SqlDataProvider.CreateInstance();
            var cmd = _db.DB.GetStoredProcCommand("Proc_Article_SelectAll");
            _db.DB.DiscoverParameters(cmd);

            cmd.Parameters["@Culture"].Value = culture;
            cmd.Parameters["@PageIndex"].Value = pageIndex;
            cmd.Parameters["@PageSize"].Value = pageSize;
            var lst = _db.DataReaderToCollection<Article>(_db.DB.ExecuteReader(cmd));

            totalCount = (int)cmd.Parameters["@RETURN_VALUE"].Value;
            return lst;
        }

        public IList<Article> GetByExcludeSlug(string slug, string taxonomySlug)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Article>("Proc_Article_SelectByExcludeSlug", slug, taxonomySlug);
        }

        public IList<Article> GetAllByTaxonomyID(Guid taxonomyID, string culture)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Article>("Proc_Article_SelectByTaxonomyId", taxonomyID, culture);
        }

        public IList<Article> GetAllByTaxonomySlug(string taxonomySlug, string culture)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Article>("Proc_Article_SelectByTaxonomySlug", taxonomySlug, culture);
        }

        public void Insert(Article entity)
        {
            SqlDataProvider.CreateInstance().ExecuteNonQuery("Proc_Article_Insert", entity);
        }

        public void Delete(Article entity)
        {
            SqlDataProvider.CreateInstance().ExecuteNonQuery("Proc_Article_Delete", entity);
        }

        public void Update(Article entity)
        {
            SqlDataProvider.CreateInstance().ExecuteNonQuery("Proc_Article_Update", entity);
        }

        #endregion
    }
}
